using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;

namespace ACM.cmsarea08.Controls
{
    public class SuggestHandler: System.Web.IHttpHandler
    {
        public bool IsReusable { get { return true; } }
        public void ProcessRequest(System.Web.HttpContext contesto)
        {
        	try {
        	
            string q = "";
            string r = "";
            int l = 5;
            //contesto.Response.ContentType = "application/json";
            
            if (contesto.Request.Params["query"] == null) { 
            	/*contesto.Response.Write("null");*/ 
            	ACM.cmsarea08.mainconfiguration.MainLogger.logger.SetLogUnique("NO QUERY","SUGGESTHANDLER","SUGHANDLER");
            	contesto.Response.Write("['no query']");return; }
            else q = HttpUtility.UrlDecode(contesto.Request.Params["query"].ToString());
            if (contesto.Request.Params["dynpart"] == null) { 
            	ACM.cmsarea08.mainconfiguration.MainLogger.logger.SetLogUnique("NO DYN PART","SUGGESTHANDLER","SUGHANDLER");
            	/*contesto.Response.Write("null");*/ contesto.Response.Write("['no dynpart']"); 
            	return; }
            else r = HttpUtility.UrlDecode(contesto.Request.Params["dynpart"].ToString());

            
            
           	ACM.cmsarea08.mainconfiguration.MainLogger.logger.SetLogUnique("HO QUERY:" + 
                                                                           q + " - DYN :" + 
                                                                           r
                                                                           ,"SUGGESTHANDLER","SUGHANDLER");
            
            //if (contesto.Request.Params["l"] != null) 
            //    Int32.TryParse(contesto.Request.Params["l"].ToString(),out l);
            //valore di default
            if (l == -1) l = 10;
            if (q.IndexOf("limit") == -1)
            {
                if (q.EndsWith(";")) q = q.Substring(0, q.IndexOf(";"));
                q = q + " limit " + l + ";";
            }
			
            object parametro = null;
            parametro = contesto.Request.Form[r];
            if(parametro==null) {
	           	ACM.cmsarea08.mainconfiguration.MainLogger.logger.SetLogUnique("NO PARAM:" + 
					r + " -- " + string.Join(", " ,contesto.Request.Form.AllKeys) 
	               ,"SUGGESTHANDLER","SUGHANDLER");
	            	contesto.Response.Write("['no parametro']");  return; }
            parametro = parametro.ToString();
            q = q.Replace(r,parametro.ToString());

           	ACM.cmsarea08.mainconfiguration.MainLogger.logger.SetLogUnique("HO QUERY:" + 
               q + " - DYN :" + 
               parametro
               ,"SUGGESTHANDLER","SUGHANDLER");
            
            
            DataSet ds = ACM.cmsarea08.DBManager.mainConnection.Helper_Dataset(q);
            if (ds == null) {contesto.Response.Write("['no set']");  return; }
            string reso = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string t = "";
                if (ds.Tables[0].Rows[i][0] is byte[])
                    t = System.Text.Encoding.UTF8.GetString((byte[])ds.Tables[0].Rows[i][0]);
                else
                    t = ds.Tables[0].Rows[i][0].ToString();
                reso += "'" + t + "',";
            }
            ds = null;
            if (reso.EndsWith(",")) reso = reso.Substring(0, reso.Length - 1);
            
            contesto.Response.Write("[" + reso + "]");
            
        	} catch (Exception ex) {
        		ACM.cmsarea08.mainconfiguration.MainLogger.logger.SetLogUnique("ERRORE:" + 
        		                                                               ex.ToString()
        		                                                               ,"SUGGESTHANDLER","SUGHANDLER");
        		
        	}
        }

    }
}
